package com.topisv.tms.utils.rule;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.topisv.tms.entity.Customers;
import com.topisv.tms.utils.DateUtils;
import com.topisv.tms.utils.InitProperties;
import com.topisv.tms.utils.enums.StatusEnums;

public class TMSTransportRule {

	private static String start_letter = "^\\D+";// 匹配不是以数字开头的

	private static String end_letter = "\\D+$";// 匹配不是以数字开头的

	private static AtomicInteger id = new AtomicInteger(10);
	
	private static AtomicLong entryId = new AtomicLong(0);
	
	public final static String CLUSTERS_NUMBER = InitProperties.get("clustersNumber");

	/**
	 * 找出以字母开头
	 * 
	 * @param item
	 * @return
	 */
	private static String getStartNumber(String item, String regx) {
		Pattern patternOne = Pattern.compile(regx);
		Matcher matcherOne = patternOne.matcher(item);
		while (matcherOne.find()) {
			return matcherOne.group();
		}
		return "";
	}

	/**
	 * 递增的运单号
	 * 
	 * @param startExpress
	 * @return
	 * @throws Exception
	 */
	private static String getExpress(String startExpress) throws Exception {
		try {
			String startNumber = getStartNumber(startExpress, start_letter);// 得到起始字母
			String bodyExpress = startExpress.substring(startNumber.length());// 得到中间数字部分
			String endNumber = getStartNumber(bodyExpress, end_letter);// 得到后缀字母
			bodyExpress = bodyExpress.substring(0, bodyExpress.length() - endNumber.length());
			return startNumber + getNextExpress(bodyExpress) + endNumber;
		} catch (Exception e) {
			throw e;
		}
	}

	/**
	 * 以1递增
	 * 
	 * @param firstExpress
	 * @return
	 */
	private static String getNextExpress(String firstExpress) {
		String indexString = "0";
		boolean indexWith = firstExpress.startsWith(indexString);
		while (indexWith) {
			indexString += "0";
			indexWith = firstExpress.startsWith(indexString);
		}
		String parse = firstExpress.substring(indexString.length() - 1);
		Long temp = Long.parseLong(parse);
		temp++;
		if (temp == 0) {
			throw new IllegalArgumentException();
		}
		return firstExpress.substring(0, indexString.length() - 1) + temp;
	}

	/**
	 * 得到新的能容订单号
	 * @param customers
	 * @return
	 * @throws Exception
	 */
	public static String getStartOrdeno(Customers customers) throws Exception {
		try {
			String startOrdeno = getExpress(customers.getStartOrdeno());
			customers.setStartOrdeno(startOrdeno);
			return StatusEnums.PARAM_PD.getStatus() + startOrdeno;
		} catch (Exception e) {
			throw e;
		}
	}

	/**
	 * 根据运单号前缀与后缀获得运单号
	 * @param startNumber
	 * @param endNumnber
	 * @return
	 * @throws Exception
	 */
	public static String getExpress(Customers customers) throws Exception {
		try {
			String endNumber = getExpress(customers.getEndNumber());
			customers.setEndNumber(endNumber);
			return StatusEnums.PARAM_PY.getStatus() + customers.getStartNumber() + endNumber;
		} catch (Exception e) {
			throw e;
		}
	}

	/**
	 * 根据运单号前缀与后缀获得运单号
	 * @param startNumber
	 * @param endNumnber
	 * @return
	 * @throws Exception
	 */
	public static String getExpress(String startNumber, String endNumnber) throws Exception {
		try {
			String expressOne = startNumber + endNumnber;
			if (expressOne.length() != 14) {
				throw new Exception("运单号:" + startNumber + "====" + endNumnber + "长度不正确");
			}
			expressOne = startNumber + getExpress(endNumnber);
			if (expressOne.length() != 14) {
				throw new Exception("运单号:" + startNumber + "====" + getExpress(endNumnber) + "长度不正确");
			}
			return expressOne;
		} catch (Exception e) {
			throw e;
		}
	}

	/**
	 * 生成入库单号
	 * @return String
	 */
	public static String generateInStorageBillCode() {
		return generateByPreDate("RK");
	}

	/**
	 * 预约单
	 * @return String
	 */
	public static String generatePreStorageBillCode() {
		return generateByPreFix("PRE");
	}

	/**
	 * 盘点编码
	 * @return String
	 */
	public static String generatePlanCode() {
		return generateByPreFix("CP");
	}

	/**
	 * 校验编码
	 * @return String
	 */
	public static String generateFixTaskCode() {
		return generateByPreFix("CF");
	}

	/**
	 * 冲正单号
	 * @return String
	 */
	public static String generateReverseBillCode() {
		return generateByPreFix("R");
	}

	/**
	 * 盘点点数编码
	 * @return String
	 */
	public static String generateCheckCountCode() {
		return generateByPreFix("CC");
	}

	/**
	 * 入库点数编码
	 * @return String
	 */
	public static String generateInstorageCountCode() {
		return generateByPreFix("C");
	}

	/**
	 * 出库单编码
	 * @return String
	 */
	public static String generateOutstorageBillCode() {
		return generateByPreFix("CK");
	}

	/**
	 * 捡货任务编码
	 * @return String
	 */
	public static String generatePickTaskCode() {
		return generateByPreFix("PICK");
	}

	/**
	 * 捡货任务编码
	 * @return String
	 */
	public static String generateOutSendTaskCode() {
		return generateByPreFix("S");
	}

	/**
	 * 线下订单编码
	 * @return String
	 */
	public static String generateULBillCode() {
		return generateByPreFixAndSuffix("PD", "UL");
	}

	/**
	 * 预包装编码
	 * @return String
	 */
	public static String generatePreTaskCode() {
		return generateByPreFix("PKG");
	}

	/**
	 * 预包装上架任务编码
	 * @return String
	 */
	public static String generateUpPreTaskCode() {
		return generateByPreFix("UP");
	}

	/**
	 * 预包装捡货任务编码
	 * @return String
	 */
	public static String generatePrePickTaskCode() {
		return generateByPreFix("P");
	}

	/**
	 * 预包装计划编码
	 * @return String
	 */
	public static String generatePrePlanCode() {
		return generateByPreFix("PRE");
	}

	/**
	 * 生成订单号
	 * @return String
	 */
	public static String generateOrderNo() {
		return generateByPreFix(StatusEnums.PARAM_PD.getStatus());
	}

	/**
	 * 生成能容运单号
	 * @return String
	 */
	public static String generateWaybillNo() {
		return generateByPreFix(StatusEnums.PARAM_PY.getStatus());
	}

	/**
	 * 上架任务号
	 * @return String
	 */
	public static String generateDispatchTaskCode() {
		return generateByPreFix("DT");
	}
	
	/**
	 * 上架任务子号
	 * @return String
	 */
	public static String generateDispatchSubTaskCode() {
		return generateByPreFix("DST");
	}
	/**
	 * 上架任务号
	 * @return String
	 */
	public static String generateUpTaskCode() {
		return generateByPreFix("US");
	}
	
	/**
	 * 快递交接 装车流水号
	 */
	public static String generateSendOutCode() {
		return generateByPreFix("SD");
	}

	/**
	 * 按前缀+纳秒时间戳生成入库单编码
	 * @param preFix
	 * @return String
	 */
	public static String generateByPreDate(String preFix) {
		return new StringBuilder().append(preFix).append(DateUtils.curDateStr()).append(CLUSTERS_NUMBER).append(entryId.getAndIncrement()).toString();
	}
	/**
	 * 按前缀+纳秒时间戳生成编码
	 * @param preFix
	 * @return String
	 */
	public static String generateByPreFix(String preFix) {
		return new StringBuilder().append(preFix).append(System.currentTimeMillis()).append(CLUSTERS_NUMBER).append(getId()).toString();
	}
	
	/**
     * 按前缀+纳秒时间戳生成编码,截取指定的长度
     * @param preFix
     * @return String
     */
    public static String generateByPreFixCut(String preFix,int fromIndex) {
        String tmpTimeMillis =(System.currentTimeMillis()+"").substring(fromIndex);
        return new StringBuilder().append(preFix).append(tmpTimeMillis).append(CLUSTERS_NUMBER).append(getId()).toString();
    }
    
	/**
	 *按前缀+纳秒时间戳生成编码+后缀生成编码
	 * @param preFix
	 * @param suffix
	 * @return String
	 */
	public static String generateByPreFixAndSuffix(String preFix, String suffix) {
		return new StringBuilder().append(preFix).append(System.currentTimeMillis()).append(getId()).append(suffix).toString();
	}

	private static int getId() {
		id.compareAndSet(99, 10);
		return id.incrementAndGet();
	}
}
